package com.easondongh.common.shiro.realm;

import com.easondongh.system.common.ActiveUser;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysUser;
import com.easondongh.system.service.SysPermissionService;
import com.easondongh.system.service.SysRoleService;
import com.easondongh.system.service.SysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * SysUser的认证与授权
 *
 * @author EasonDongH
 * @date 2020/4/28 16:44
 */
public class SysUserRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysRoleService roleService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info =new SimpleAuthorizationInfo();
        ActiveUser activeUser= (ActiveUser) principals.getPrimaryPrincipal();
        List<String> permissions = activeUser.getPermissions();
        List<String> roles = activeUser.getRoles();
        SysUser user = activeUser.getUser();
        if(user.getType().equals(Constants.SUPER_ADMIN)){
            info.addStringPermission("*:*");
        }else{
            if(!CollectionUtils.isEmpty(roles)){
                info.addRoles(roles);
            }
            if(!CollectionUtils.isEmpty(permissions)){
                info.addStringPermissions(permissions);
            }
        }
        return info;
    }

    /**
     * 登录认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = token.getPrincipal().toString();
        SysUser user = this.userService.queryUserByLoginName(userName);
        if (null != user) {
            ActiveUser activeUser = new ActiveUser();
            activeUser.setUser(user);
            List<String> roles = this.roleService.getRoleNamesByUid(user.getId());
            List<String> permissions = this.permissionService.getPermissionUriByUid(user.getId());
            activeUser.setRoles(roles);
            activeUser.setPermissions(permissions);
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(activeUser, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
            return info;
        }
        return null;
    }
}
